package main
import (
	"fmt"
)

//  13. Maps
func main(){

    var personSalary map[string]int

    if personSalary == nil {
        fmt.Println("map is nil. Going to make one.")
        fmt.Println(personSalary)
        personSalary = make(map[string]int)
        fmt.Println(personSalary)
    }

    personSalary1 := make(map[string]int)
    personSalary1["yang"] = 10000
    personSalary1["xiao"] = 20000
    personSalary1["hao"] = 30000
    fmt.Println("\npersonSalary1=", personSalary1)

    // 声明初始化
    personSalary2 := map[string]int {
        "yang": 100,
        "xiao": 200,
    }
    personSalary2["hao"] = 300
    personSalary2["xiao"] = 211
    fmt.Println("personSalary2= ", personSalary2)

    // 获取 map的值
    personSalary3 := map[string]int{
        "steve": 12000,
        "jamie": 15000,
    }
    personSalary3["yanghao"] = 80000
    employee := "jamie"
    fmt.Println("employee= ", employee,"is ", personSalary3[employee],"----",personSalary3["yangxiaohao"])
    // 获取 map 中某个 key 是否存在的语法 存在ok== true value是对应的值
    value, ok := personSalary3["yanghao"]
    if ok == true {
        fmt.Println("ok --- value=", value)
    } else {
        fmt.Println("value=", value)
    }

    // 遍历 map 不保证每次执行程序获取的元素顺序相同。
    for key, value := range personSalary3 {
        fmt.Printf("personSalary[%s] = %d\n", key, value)
    }
    // 删除 map 中的元素
    fmt.Println("\npersonSalary3=", personSalary3)
    delete(personSalary3, "jamie")
    fmt.Println("personSalary3=", personSalary3)
    // 获取 map 的长度 len
    fmt.Println("长度： ",len(personSalary3))
    // Map 是引用类型
    personSalary3["mike"] = 9000
    fmt.Println("\npersonSalary3=", personSalary3)
    personSalary3["mike"] = 999000
    fmt.Println("personSalary3=", personSalary3)

    // Map 的相等性
    // map1 := map[string]int{
    //     "one": 1,
    //     "two": 2,
    // }
    // map2 := map1
    // if map1 == map2 {   //编译错误
    // }







}
